/*******************************************************************
 * author   :  Charlies-ls
 * Date     :  2021-09-26
 * Copyright (c) 2021. artestplay@outlook.com. All Rights Reserved.
 *******************************************************************/
#include <stdlib.h>
#include "com.h"
#include "ax_list.h"
#include "com.h"
#include "ax_queue.h"
#include "ax_printf.h"

void ax_queue_push_back(ax_queue* q, void* obj)
{
    ax_check_and_return(q && obj, "error");
    ax_list_push_back_data(q->list,obj);
}

void ax_queue_push_front(ax_queue*q,void*obj)
{
    ax_check_and_return(q && obj, "error");
    ax_list_add_push_front_data(q->list,obj);
}

/**
 * @brief return front data and free the node
 * @param  q
 * @return data
 */
void*  ax_queue_pop_front(ax_queue*q)
{
  ax_check_and_return_zero(q, "error");
  ax_list_node* node = ax_list_del(q->list,q->list->head);
  ax_check_and_return_zero(node, "empty");
  void* data = node->data;
  free(node);
  return data;
}

/**
 * @brief return back data and free the node
 * @param q
 * @return
 */
void*  ax_queue_pop_back(ax_queue*q)
{
   ax_check_and_return_zero(q, "error");
   ax_list_node* node = ax_list_del(q->list,q->list->tail);
   ax_check_and_return_zero(node, "empty");
   void* data = node->data;
   free(node);
   return data;
} 

int ax_queue_count(ax_queue*q)
{
  ax_check_and_return_zero(q, "empty");
  return ax_list_size(q->list);
}

void* ax_queue_front(ax_queue*q)
{
  ax_check_and_return_zero(q, "error");
  return ax_list_front(q->list);
}

void *ax_queue_back(ax_queue *q)
{
    ax_check_and_return_zero(q, "error");
    return ax_list_back(q->list);
}


ax_queue* ax_queue_create()
{
    ax_queue* q = ALLOC(ax_queue);
    q->list = ax_list_create();
    return q;
}

void ax_queue_destory(ax_queue*q)
{
    ax_check_and_return(q, "empty");
    ax_list_destory(q->list);
    free(q);
}


